/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.wicket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import dto.ExtendedOrder;
/**
 *
 * @author akulyk
 */
public class OrderProvider extends SortableDataProvider<ExtendedOrder> 
{
    private List<ExtendedOrder> oList;
    
    public OrderProvider(List<ExtendedOrder> oList)
    {
        setSort("id",true);
        this.oList=oList;
    }
    
    public Iterator<? extends ExtendedOrder> iterator(int first, int count)
    {
        List<ExtendedOrder>data=new ArrayList<ExtendedOrder>(oList);
        Collections.sort(data,new Comparator<ExtendedOrder>()
                                {
                                    public int compare(ExtendedOrder o1, ExtendedOrder o2)
                                    {
                                        int dir = getSort().isAscending() ? 1 : -1;
                                        if ("orderName".equals(getSort().getProperty())) {
						return dir * (o1.orderName.compareTo(o2.orderName));
					} else {
						return dir * ( Double.compare(o1.totalPrice,o2.totalPrice));
					}
                                    }
                                });
        return data.subList(first, Math.min(first + count, data.size())).iterator();
    }
    public int size()
    {
        return oList.size();
    }

    public IModel<ExtendedOrder> model(ExtendedOrder object) 
    {
        return Model.of(object);
    }
}
